package main

import (
	"encoding/json"
	"fmt"
)

type Student struct {
	Id     int
	Name   string
	Age    int
	gender string //gender是小写,小写字母开头的，在json编码时会忽略掉
}

func main() {
	cc := Student{
		Id:     1,
		Name:   "CC",
		Age:    18,
		gender: "female",
	}

	//编码（序列化），结构 ===》字符串
	//func Marshal(v interface{}) ([]byte, error) {
	encodeInfo, err := json.Marshal(&cc)
	if err != nil {
		fmt.Println("json.Marshal err: ", err)
	}

	fmt.Println("encodeInfo: ", string(encodeInfo))

	//反序列化（解码），字符串=》结构体
	var cc2 Student
	//func Unmarshal(data []byte, v interface{}) error {
	if err := json.Unmarshal(encodeInfo, &cc2); err != nil {
		fmt.Println("json unmarshall failed, err: ", err)
		return
	}

	fmt.Println("name: ", cc2.Name)
	fmt.Println("gende: ", cc2.gender)
	fmt.Println("age: ", cc2.Age)
	fmt.Println("Id: ", cc2.Id)

}
